import { Snake } from '../entities/Snake';

export type SkinType = 'default' | 'neon' | 'metal' | 'spots' | 'ghost';

export interface SkinInfo {
    id: SkinType;
    name: string;
    description: string;
    requiredScore: number;
}

export class SkinSystem {
    private currentSkin: SkinType = 'default';
    private skins: Map<SkinType, SkinInfo>;
    private snake: Snake | null = null;

    constructor() {
        this.skins = new Map();
        this.initializeSkins();
        this.loadSavedSkin();
    }

    private initializeSkins(): void {
        this.skins.set('default', {
            id: 'default',
            name: '经典绿色',
            description: '传统贪吃蛇的经典造型',
            requiredScore: 0
        });

        this.skins.set('neon', {
            id: 'neon',
            name: '霓虹渐变',
            description: '炫酷的霓虹灯效果',
            requiredScore: 1000
        });

        this.skins.set('metal', {
            id: 'metal',
            name: '金属条纹',
            description: '坚硬的金属质感',
            requiredScore: 2000
        });

        this.skins.set('spots', {
            id: 'spots',
            name: '动态斑点',
            description: '活力四射的斑点图案',
            requiredScore: 3000
        });

        this.skins.set('ghost', {
            id: 'ghost',
            name: '半透明幻影',
            description: '神秘的透明效果',
            requiredScore: 4000
        });
    }

    public setSnake(snake: Snake): void {
        this.snake = snake;
        this.applySkin(this.currentSkin);
    }

    public applySkin(skinName: SkinType): void {
        if (!this.skins.has(skinName)) return;

        this.currentSkin = skinName;
        localStorage.setItem('snakeSkin', skinName);

        // 通知渲染器更新皮肤
        document.dispatchEvent(new CustomEvent('skinChanged', {
            detail: { skin: skinName }
        }));
    }

    private loadSavedSkin(): void {
        const savedSkin = localStorage.getItem('snakeSkin') as SkinType || 'default';
        this.applySkin(savedSkin);
    }

    public getCurrentSkin(): SkinType {
        return this.currentSkin;
    }

    public getAvailableSkins(): SkinInfo[] {
        return Array.from(this.skins.values());
    }

    public isSkinUnlocked(skinName: SkinType, score: number): boolean {
        const skin = this.skins.get(skinName);
        return skin ? score >= skin.requiredScore : false;
    }
}